#include "DeletedClient.h"
#include "ui_DeletedClient.h"

extern QSqlDatabase enjoylifedb;
extern DeletedClientSqlTableModel *deletedClient_model;

DeletedClient::DeletedClient(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeletedClient)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowMinimizeButtonHint);
    setWindowFlags(Qt::WindowCloseButtonHint);

    // Modelo correspondiente a la tabla "cliente_eliminado"
    deletedClient_model = new DeletedClientSqlTableModel(this, enjoylifedb);
    deletedClient_model->setTable("cliente_eliminado");
    deletedClient_model->setEditStrategy(DeletedClientSqlTableModel::OnFieldChange);

    SelectDeletedClients();

    // Definición de acceso directo para Cancelar cambios.".
    QShortcut* shortcutCancelar = new QShortcut(QKeySequence(Qt::Key_Escape), this);
    connect(shortcutCancelar, SIGNAL(activated()), this, SLOT(close()));
}

/*
 * Destructor de la clase DeletedClient
 */
DeletedClient::~DeletedClient()
{
    delete ui;
}

/*
 * Invoca el comando sql Select para recuperar los registros de la tabla cliente_elimnado
 */
void DeletedClient::SelectDeletedClients()
{
    deletedClient_model->setTable("cliente_eliminado");
    enjoylifedb.transaction();
    if (!(deletedClient_model->select())) {
        QMessageBox::critical(this,"Error!", "No se pudieron recuperar los registros, intentelo manualmente.");
        enjoylifedb.rollback();
        return;
    }
    enjoylifedb.commit();

    deletedClient_model->setHeaderData(0, Qt::Horizontal, "Nro. Cliente");
    deletedClient_model->setHeaderData(1, Qt::Horizontal, "Razón Social");
    deletedClient_model->setHeaderData(2, Qt::Horizontal, "Responsable");
    deletedClient_model->setHeaderData(3, Qt::Horizontal, "Fecha Baja");
    deletedClient_model->setHeaderData(4, Qt::Horizontal, "Hora Baja");

    // Vista del modelo definido anteriormente
    ui->deletedClient_view->setModel(deletedClient_model);
    ui->deletedClient_view->sortByColumn(0,Qt::SortOrder());
    ui->deletedClient_view->setAlternatingRowColors(true);
    ui->deletedClient_view->setGridStyle(Qt::SolidLine);
    ui->deletedClient_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->deletedClient_view->setColumnWidth(0,70);
    ui->deletedClient_view->setColumnWidth(1,245);
    ui->deletedClient_view->setColumnWidth(2,245);
    ui->deletedClient_view->setColumnWidth(3,80);
    ui->deletedClient_view->setColumnWidth(4,80);
}

/*
 * Cierra la ventana
 */
void DeletedClient::on_cerrarBtn_clicked()
{
    close();
}
